package com.tzbank.back.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tzbank.back.service.AuthService;
import com.tzbank.entity.backstage.Auth;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class AuthController {

	@Autowired
	private AuthService authService;

	/**
	 * 添加权限
	 * @param request
	 * @param auth
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年5月31日
	 */
	@PostMapping(value = "addAuth")
	public Map<String, Object> addAuth(HttpServletRequest request, @RequestBody Auth auth) {
		try {
			return this.authService.insertAuth(auth);
		} catch (Exception e) {
			log.error("AuthController addAuth error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 修改权限
	 * @param request
	 * @param auth
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年5月31日
	 */
	@PostMapping(value = "modifyAuth")
	public Map<String, Object> modifyAuth(HttpServletRequest request, @RequestBody Auth auth) {
		try {
			return this.authService.updateAuth(auth);
		} catch (Exception e) {
			log.error("AuthController modifyAuth error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 通过自增ID查询权限
	 * @param request
	 * @param autoId
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年5月31日
	 */
	@GetMapping(value = "selectAuth")
	public Map<String, Object> selectAuth(HttpServletRequest request, @RequestParam(required = true) Integer autoId) {
		try {
			return this.authService.selectAuthByAutoId(autoId);
		} catch (Exception e) {
			log.error("AuthController selectAuth error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 查询权限
	 * @param request
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年5月31日
	 */
	@GetMapping(value = "selectAuthList")
	public Map<String, Object> selectAuthList(HttpServletRequest request) {
		try {
			return this.authService.selectAuthList();
		} catch (Exception e) {
			log.error("AuthController selectAuthList error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 删除权限
	 * @param request
	 * @param auth
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年5月31日
	 */
	@PostMapping(value = "deleteAuth")
	public Map<String, Object> deleteAuth(HttpServletRequest request, @RequestBody Auth auth) {
		try {
			return this.authService.deleteAuth(auth);
		} catch (Exception e) {
			log.error("AuthController deleteAuth error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

}
